# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=openxr-sdk
pkgbase=mingw-w64-${_realname}
pkgname=${MINGW_PACKAGE_PREFIX}-${_realname}
pkgver=1.0.27
pkgrel=4
pkgdesc='OpenXR is a royalty-free, open standard that provides high-performance access to Augmented Reality (AR) and Virtual Reality (VR) (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url='https://www.khronos.org/openxr/'
license=('spdx:Apache-2.0')
depends=("${MINGW_PACKAGE_PREFIX}-jsoncpp"
         "${MINGW_PACKAGE_PREFIX}-vulkan-loader")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-shaderc")
source=("https://github.com/KhronosGroup/OpenXR-SDK-Source/releases/download/release-${pkgver}/OpenXR-SDK-Source-release-${pkgver}.tar.gz"{,.asc}
        001-use-def-file-on-mingw-w64.patch
        002-libname-def-file.patch
        003-disable-version-script.patch
        004-fix-cmake-install-dir.patch)
sha256sums=('a52cb95fa12279773d263cdeb81fe35a7b93d40d448d22ee41bd05090e7fee2a'
            'SKIP'
            '399543a1d9f6e4c5399daa991ea9463512e361e6c334986ef8e203b63ed34364'
            'a3e8aacb197336ac707f4871757ad56758a1753d2ca630678ed24b2df7d33bbf'
            '66f71a382a27c82d17ea5425e61b18503cecc2a52b9b8c6561344d7a562c070e'
            'a03fc2adf3a3f083c92bb44e76511d22c6ad0ac5ab212ebce68e6dcbb01c72e7')
validpgpkeys=(F029455EAB70F520FF4A03BD7F534CD56F00321E) # Ryan Andrew Pavlik <ryan.pavlik@collabora.com>

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying $_patch"
    patch -Np1 -i "${srcdir}/$_patch"
  done
}

prepare() {
  cd ${srcdir}/OpenXR-SDK-Source-release-${pkgver}

  apply_patch_with_msg \
    001-use-def-file-on-mingw-w64.patch \
    002-libname-def-file.patch \
    003-disable-version-script.patch \
    004-fix-cmake-install-dir.patch
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  export VULKAN_SDK=${MINGW_PREFIX}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    "${_extra_config[@]}" \
    -DDYNAMIC_LOADER=ON \
    -DBUILD_WITH_SYSTEM_JSONCPP=ON \
    -DBUILD_TESTS=OFF \
    ../OpenXR-SDK-Source-release-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  cd ${srcdir}/build-${MSYSTEM}
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

  install -Dm644 "${srcdir}"/OpenXR-SDK-Source-release-${pkgver}/LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
